package com.example.resource;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

//整个应用的管理类
public class MyApplication  extends Application {

    private static final String TAG = MyApplication.class.getSimpleName();

    private static MyApplication INSTANCE; //Application的实例

    public static MyApplication getInstance() { //获取应用实例的方法，是在其它界面调用的
        return INSTANCE;
    }

    @Override
    public void onCreate() {//应用创建的方法
        super.onCreate();
        INSTANCE=this; //在创建MyApplication实例之后赋值给INSTANCE
        Log.i(TAG,"MyApplication的onCreate已经运行");

        registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());

    }


    //registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks())
    /**
     * Activity生命周期回调接口实现
     */
    class MyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
        public final String TAG = MyActivityLifecycleCallbacks.class.getSimpleName();

        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onActivityCreated: " + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {
            Log.i(TAG, "onActivityStarted: " + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            Log.i(TAG, "onActivityResumed: " + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            Log.i(TAG, "onActivityPaused: " + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {
            Log.i(TAG, "onActivityStopped: " + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
            Log.i(TAG, "onActivitySaveInstanceState: " + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
            Log.i(TAG, "onActivityDestroyed: " + activity.getClass().getSimpleName());
        }
    }
}
